<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<div class="" style="padding: 5px;"> 
	<button id="id-btn-dialog1" class="btn btn-sm btn-primary">增加卷</button>
	<button id="id-btn-dialog2" class="btn btn-sm btn-primary">编辑卷</button>
	<button id="id-btn-dialog3" class="btn btn-sm btn-primary">删除卷</button> 
	<div style="float:right;">
		<input type="text" id="serch_text" style="width:250px" placeholder="jgh">
	</div>
</div>
<table class="table table-striped table-hover table-bordered">
		  <thead>
			<tr id="sys_sys_TestPaper_table_title">
			  <th  width="50">编号</th>   
			  <th  width="20" fname="sys_TestPaperNameCheckBox"><input id="sys_TestPaperNameCheckBox" type="checkbox"></th>
			  <th  width="60" fname="name">卷名称</th> 
			  <th  width="60" fname="note">卷描述</th> 
			  <th  width="60" fname="type">卷类型</th> 
			  <th  width="60" fname="showStartTime">开始时间</th> 
			  <th  width="100" fname="showEndTime">结束时间</th> 
			</tr>
		  </thead>
		  <tbody id="sys_sys_TestPaper_table">
			 
		  </tbody>
</table> 
<ul class="pagination" style ="margin:0 auto;"id="sys_TestPaper_pagination">
					  
</ul> 	
<div class="" style="padding: 5px;"> 
	<button id="id-btn-dialog4" class="btn btn-sm btn-success">增加题</button>
	<button id="id-btn-dialog5" class="btn btn-sm btn-success">编辑题</button>
	<button id="id-btn-dialog6" class="btn btn-sm btn-success">删除题</button>  
</div> 
<table class="table table-striped table-hover table-bordered">
		  <thead>
			<tr id="sys_sys_Question_table_title">
			  <th  width="50">编号</th>   
			  <th  width="20" fname="sys_QuestionNameCheckBox"><input id="sys_QuestionNameCheckBox" type="checkbox"></th>
			  <th  width="60" fname="type">类型</th> 
			  <th  width="60" fname="title">名称</th> 
			  <th  width="60" fname="content">内容</th> 
			  <th  width="60" fname="fen">分值</th>
			  <th  width="60" fname="answerTeacher">答案</th>
			</tr>
		  </thead>
		  <tbody id="sys_sys_Question_table">
			 
		  </tbody>
</table>  	
<script type="text/javascript">
(function(){
	
	//增加卷
	$("#id-btn-dialog1").click(function(){
		$("#user_include").html("");
	 	$("#user_include").load("${pageContext.request.contextPath}/homeWorkAdd.jsp?r="+Math.random(),{},function(){
	 		var dialog = $("#dialog-message").removeClass('hide').dialog({
				modal: true,
				title: "增加劵",
				width:800,
				height:500,
				close:function(){
					$(this).dialog('destroy'); 
					$("#user_include").html("");
				},
				title_html: true,
				buttons: [  
					{
						text: "提交",
						"class" : "btn btn-primary btn-xs",
						click: function() {
							 window.homeWorkAdd.addHomeWork(this);
							 //loadTestPaper(null,{});
						} 
					}
				]
			}); 
		}); 
	});
	//修改卷
	$("#id-btn-dialog2").click(function(e){
			e.preventDefault();
			var allc =$("input[type=checkbox][name=sys_sys_TestPaper_table_checkbox]:checked");
			if(allc.length>1||allc.length<=0){
				alert("只能选中一行进行操作!");
				return;
			}
			var table_id=$(allc[0]).parent().parent().attr("id");
			var cdata =$("#"+table_id).data(table_id);
		$("#user_include").html("");
	 	$("#user_include").load("${pageContext.request.contextPath}/homeWorkAdd.jsp?r="+Math.random(),cdata,function(){
	 		var dialog = $("#dialog-message").removeClass('hide').dialog({
				modal: true,
				title: "增加劵",
				width:800,
				height:500,
				close:function(){
					$(this).dialog('destroy'); 
					$("#user_include").html("");
				},
				title_html: true,
				buttons: [  
					{
						text: "提交",
						"class" : "btn btn-primary btn-xs",
						click: function() {
							 window.homeWorkAdd.addHomeWork(this);
							 //loadTestPaper(null,{});
						} 
					}
				]
			}); 
		}); 
	});
	//增加题
	$("#id-btn-dialog4").click(function(e){
			e.preventDefault();
			var allc =$("input[type=checkbox][name=sys_sys_TestPaper_table_checkbox]:checked");
			if(allc.length>1||allc.length<=0){
				alert("你没有选中卷");
				return;
			}
			var table_id=$(allc[0]).parent().parent().attr("id");
			var cdata =$("#"+table_id).data(table_id);
		$("#user_include").html("");
	 	$("#user_include").load("${pageContext.request.contextPath}/homeWorkQuestion.jsp?f=1&r="+Math.random(),cdata,function(){
	 		var dialog = $("#dialog-message").removeClass('hide').dialog({
				modal: true,
				title: "增加题",
				width:800,
				height:600,
				close:function(){
					$(this).dialog('destroy'); 
					$("#user_include").html("");
				},
				title_html: true,
				buttons: [  
					{
						text: "提交",
						"class" : "btn btn-primary btn-xs",
						click: function() {
							 window.homeWorkQuestion.addHomeWorkQuestion(this);
							 //loadTestPaper(null,{});
						} 
					}
				]
			}); 
		}); 
	});
	//用户搜索
	function loadTestPaper(event,data){
		if(typeof(data)=="undefined"){
			data={};
		}
		var url ="${pageContext.request.contextPath}/edu/sys_TestPaperAction.do?method=getTestPaper";
		data.name=$("#serch_text").val(); 
		var cp =$("#sys_TestPaper_pagination").find(".active");
		data.start=1;
		data.limit=5;
		if(cp.length>0){
			var ccp=$(cp[0]).text();
			if(parseInt(ccp)){
				data.start=ccp;
			}
		}  
		$("#table_TestPaper").wgrid({
					table_title:"sys_sys_TestPaper_table_title",
					tbodyId:"sys_sys_TestPaper_table",
					pagination:"sys_TestPaper_pagination",
					url:url,
					checkbox:true,
					data:data,
					success:function(){
						changeTestType();
						changeUserState();
					}
		});
	};
	//卷子的中的题
	function loadTestPaperQuestion(event,data){
		if(typeof(data)=="undefined"){
			data={};
		}
		var url ="${pageContext.request.contextPath}/edu/sys_TestPaperAction.do?method=getTestPaperQuestion";
		data.name=$("#serch_text").val(); 		 
		var cp =$("#sys_TestPaper_pagination").find(".active");
		data.start=1;
		if(cp.length>0){
			var ccp=$(cp[0]).text();
			if(parseInt(ccp)){
				data.start=ccp;
			}
		}  
		$("#table_TestPaper").wgrid({
					table_title:"sys_sys_Question_table_title",
					tbodyId:"sys_sys_Question_table",
					pagination:"sys_sys_Question_pagination",
					url:url,
					checkbox:true,
					data:data,
					success:function(){
						changeQuestionType();
						changeQuectionState();
					}
		});
	};
	//全选中事件
	$("#sys_TestPaperNameCheckBox").click(function(){
		var allc =$("[name=sys_sys_TestPaper_table_checkbox]");
		for(var i=0;i<allc.length;i++){
			$(allc[i])[0].checked=$(this)[0].checked;
		}
			 
	});

	//改变他的启用禁用的状态
	function changeUserState(){ 
		$("#sys_sys_TestPaper_table").children().click(function(){
			 var allc =$("[name=sys_sys_TestPaper_table_checkbox]");
			 if(allc.length>0){
				 for(var i=0;i<allc.length;i++){
					 $(allc[i])[0].checked =false;
				 }
			 }
			if($(this).find("[name=sys_sys_TestPaper_table_checkbox]")[0].checked==true){
				$(this).find("[name=sys_sys_TestPaper_table_checkbox]")[0].checked=false;
			}else{
				$(this).find("[name=sys_sys_TestPaper_table_checkbox]")[0].checked=true;
				var table_id =$(this).attr("id");
				loadTestPaperQuestion(null,{table_id:table_id});
				//调用显示题的功能
			}
		});
	};
	
	//调整问题的类型
	function changeQuestionType(){
		var allstate =$("div[name='sys_sys_Question_table_type_tr_td']");
		for(var i=0;i<allstate.length;i++){
			var bc = $(allstate[i]);
			if(bc.text()==1){
				bc.html('<font color="green">客观题</font>');
			}else if(bc.text()==0){
				bc.html('<font color="red">主观题</font>');
			}
		} 
	};
	//全选中事件
	$("#sys_QuestionNameCheckBox").click(function(){
		var allc =$("[name=sys_sys_Question_table_checkbox]");
		for(var i=0;i<allc.length;i++){
			$(allc[i])[0].checked=$(this)[0].checked;
		}
			 
	});
	//改变他的启用禁用的状态
	function changeQuectionState(){ 
		$("#sys_sys_Question_table").children().click(function(){
			if($(this).find("[name=sys_sys_Question_table_checkbox]")[0].checked==true){
				$(this).find("[name=sys_sys_Question_table_checkbox]")[0].checked=false;
			}else{
				$(this).find("[name=sys_sys_Question_table_checkbox]")[0].checked=true;
				var table_id =$(this).attr("id");
				 
				//loadTestPaperQuestion(null,{table_id:table_id});
				//调用显示题的功能
			}
		});
	};
	//改变头像 和性别，还有类型
	function changeTestType(){ 
		
		var allstate =$("div[name='sys_sys_TestPaper_table_type_tr_td']");
		for(var i=0;i<allstate.length;i++){
			var bc = $(allstate[i]);
			if(bc.text()==1){
				bc.html('<font color="green">固定时间</font>');
			}else if(bc.text()==0){
				bc.html('<font color="red">规定时间</font>');
			}
		} 
	};
	//搜索的回车事件
	$('#serch_text').keydown(function(e){
		if(e.keyCode==13){
			loadTestPaper(null,{});
		}
	});
	//删除选中卷
	$("#id-btn-dialog3" ).on('click', function(e) {
		e.preventDefault();
		testPaperDelete();
	});
	//删除选中题
	$("#id-btn-dialog6" ).on('click', function(e) {
		e.preventDefault();
		questionDelete();
	});
	//删除一个题
	function questionDelete(){
		var r=confirm("您确认要删除数据吗？")
		if (r==false){return}; 
		var allc =$("input[type=checkbox][name=sys_sys_Question_table_checkbox]:checked");
		if(allc.length<=0){
			alert("你没有选中卷,不能进行删除操作!");
			return;
		}
		var arr = [];
		for(var i=0;i<allc.length;i++){
			arr.push($(allc[i]).parent().parent().attr("id"));
		}
		$.ajax({
			type: "POST",
			url:"${pageContext.request.contextPath}/edu/sys_TestPaperAction.do?method=deleteQuestion",
			data:{allId:obj2str(arr)},
			dataType:'json',
			success: function(msg){
				alert(msg.message); 
				//loadTestPaper(null,{});
			}
		});	 
	};
	//删除一个卷
	function testPaperDelete(){
		var r=confirm("您确认要删除数据吗？")
		if (r==false){return}; 
		var allc =$("input[type=checkbox][name=sys_sys_TestPaper_table_checkbox]:checked");
		if(allc.length<=0){
			alert("你没有选中卷,不能进行删除操作!");
			return;
		}
		var arr = [];
		for(var i=0;i<allc.length;i++){
			arr.push($(allc[i]).parent().parent().attr("id"));
		}
		$.ajax({
			type: "POST",
			url:"${pageContext.request.contextPath}/edu/sys_TestPaperAction.do?method=deleteTestPaper",
			data:{allId:obj2str(arr)},
			dataType:'json',
			success: function(msg){
				alert(msg.message); 
				//loadTestPaper(null,{});
			}
		});	
		 
	};
	//修改题
	$("#id-btn-dialog5").click(function(e){
			e.preventDefault();
			var allc =$("input[type=checkbox][name=sys_sys_Question_table_checkbox]:checked");
			if(allc.length>1||allc.length<=0){
				alert("只能选中一行进行操作!");
				return;
			}
			var table_id=$(allc[0]).parent().parent().attr("id");
			var cdata =$("#"+table_id).data(table_id);
			//拿到当前的卷
			var allc1 =$("input[type=checkbox][name=sys_sys_TestPaper_table_checkbox]:checked");
			if(allc1.length>1||allc1.length<=0){
				alert("没有选中卷");
				return;
			}
			var table_id1=$(allc1[0]).parent().parent().attr("id");
			var cdata1 =$("#"+table_id1).data(table_id1);
			cdata.name=cdata1.name;
			cdata.juanId =cdata1.table_id;
			
		$("#user_include").html("");
	 	$("#user_include").load("${pageContext.request.contextPath}/homeWorkQuestion.jsp?f=2&r="+Math.random(),cdata,function(){
	 		var dialog = $("#dialog-message").removeClass('hide').dialog({
				modal: true,
				title: "修改题",
				width:800,
				height:500,
				close:function(){
					$(this).dialog('destroy'); 
					$("#user_include").html("");
				},
				title_html: true,
				buttons: [  
					{
						text: "提交",
						"class" : "btn btn-primary btn-xs",
						click: function() {
							 window.homeWorkQuestion.addHomeWorkQuestion(this);
							 //loadTestPaper(null,{});
						} 
					}
				]
			}); 
		}); 
	});
	window.homeWork={};
	window.homeWork.loadTestPaper=loadTestPaper;
	window.homeWork.loadTestPaperQuestion=loadTestPaperQuestion;
	//加载
	$(function(){
		loadTestPaper(null,{});
		
	});
})();
</script> 
 
 
 